//
//  MLZCommuniyDetalisViewController.m
//  MotherLover
//
//  Created by lanou on 16/1/10.
//  Copyright © 2016年 zj. All rights reserved.
//

#import "MLZCommuniyDetalisViewController.h"
#import "MLZCommuityDetalisView.h"
#import "MLZLZCommunityTableViewCell.h"
#import "MLZPLViewcell.h"
#import "HcCustomKeyboard.h"
#import "NSString+Characters.h"
#import "MLZPLModl.h"
#import "MLZCommunityHFViewController.h"
#import "MLZHFModel.h"
@interface MLZCommuniyDetalisViewController ()<UITableViewDataSource,UITableViewDelegate,MLZLZCommunityTableViewCellDelegate,UITextFieldDelegate,HcCustomKeyboardDelegate,MLZPLViewDelegate,UINavigationControllerDelegate,UIImagePickerControllerDelegate>
@property (nonatomic, strong)UITableView *DetalisTableView;
@property (nonatomic, strong)NSMutableArray *listArray;
@property (nonatomic, strong)NSMutableArray *headHeightArray;
@property (nonatomic, strong)NSMutableDictionary *HFdic;

@end

@implementation MLZCommuniyDetalisViewController
{

    CGFloat H;//cell高度
    NSString *title;//标题名字
    CGFloat HForHead;//分区表头高度
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    _headHeightArray = [NSMutableArray array];
    self.view.backgroundColor = [UIColor whiteColor];
    NSLog(@"%@",_model);
    title = _model.title;
    [self addCommunitydetalis];
    [self datapassing];
    self.tabBarController.tabBar.hidden= YES;
    [[HcCustomKeyboard customKeyboard] textViewShowView:self customKeyboardDelegate:self];
     [[HcCustomKeyboard customKeyboard].IMButton addTarget:self action:@selector(IMButtonAction) forControlEvents:UIControlEventTouchUpInside];

}




//评论界面的请求
-(void)datapassing{
    //转化成拼音状态下的表名
    NSString *ClassName = [NSString stringWithFormat:@"P%@ACM_", _model.objectId];
    BmobQuery *bquery = [[BmobQuery alloc]initWithClassName:ClassName];
    bquery.cachePolicy= kBmobCachePolicyNetworkOnly;
    //[bquery clearCachedResult];
    
    //[bquery orderByDescending:@"updatedAt"];
    bquery.limit = 20;
    _listArray = [NSMutableArray array];
    _HFdic = [NSMutableDictionary dictionary];
    [bquery calcInBackgroundWithBlock:^(NSArray *array, NSError *error) {
        for (int i = 0 ; i < array.count; i++) {
         
    NSString *ClassNameHF = [NSString stringWithFormat:@"P%@_%d_AM_",_model.objectId,i];
    BmobQuery *que = [BmobQuery queryWithClassName:ClassNameHF];
       
   [que calcInBackgroundWithBlock:^(NSArray *array, NSError *error) {
       NSMutableArray *arraydic = [NSMutableArray array];
       for (NSDictionary *onedic in array) {
           MLZHFModel *model = [[MLZHFModel alloc]init];
           [model setValuesForKeysWithDictionary:onedic];
        [arraydic addObject:model];
       }
       if (arraydic.count!=0) {
           [_HFdic setValue:arraydic forKey:[NSString stringWithFormat:@"%d",i]];
       }
       
       
       
       [self.DetalisTableView reloadData];
   }];
    }
        
        
        
       
        for (NSDictionary*onedic in array) {
          
            MLZPLModl *model = [[MLZPLModl alloc]init];
            [model setValuesForKeysWithDictionary:onedic];
            [_listArray addObject:model];
            
            
            
        }
        
        
        
        
        
        
        
    }];

   
    
        
        
        
        
   





}

#pragma mark------布局帖子详细界面
//CELL传回高度
- (void)FHfloatForCellHeigeht:(CGFloat)height{


    NSLog(@"%f",height);
    H = height;
   

}

//添加详细界面和回复楼主按钮
- (void)addCommunitydetalis{

    _DetalisTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, KW, KH-64-49) style:(UITableViewStylePlain)];
    _DetalisTableView .delegate = self;
    _DetalisTableView.dataSource= self;
    [self.view addSubview:_DetalisTableView];
   
}
//回复方法
- (void)buttonHFAction:(UIButton*)button{

   
    [button resignFirstResponder];


}
//分行
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{


    if(section==0){
        return 1;
    }else {
        NSMutableArray *array =[_HFdic objectForKey:[NSString stringWithFormat:@"%d",section-1]];
        if (array.count!=0) {
            return array.count+1;
        }else {
            return 1;
        }
    }


}
//分区
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

   
    return _listArray.count+1;
   
}
//分行的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    if(indexPath.section==0){
    
        return H;
    
    }else{
        if (indexPath.row ==0) {
        
        return HForHead;
        }else{
        
            return 20;
        }

    }



}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
 static NSString *identifier =@"ZJCell";
    static NSString *indetifierP = @"PZJcell";
    static NSString *identifierH = @"HZJCell";
    if (indexPath.section==0){
        MLZLZCommunityTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
         cell.delegate= self;
        if (!cell) {
            cell= [[MLZLZCommunityTableViewCell alloc]initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:identifier];
            
              cell.delegate= self;
        }
        
        cell.model= _model;
        return cell;

    
    
    }else{
    
        if (indexPath.row==0) {
            
        
        MLZPLViewcell *cell = [tableView dequeueReusableCellWithIdentifier:indetifierP];
        cell.delegate= self;
        if (!cell) {
            cell= [[MLZPLViewcell alloc]initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:indetifierP];
            cell.delegate= self;
        }
        if(indexPath.row==0){
            cell.labletype.text = [NSString stringWithFormat:@"%ld楼",(long)indexPath.section];
        cell.model = _listArray[indexPath.section-1];
            cell.buttonHF.tag = indexPath.section+200-1;
        [cell.buttonHF addTarget:self action:@selector(HFbuttonAction:) forControlEvents:(UIControlEventTouchUpInside)];
        }
            
        return cell;

        }else{
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifierH];
        
            if (!cell) {
                cell = [[UITableViewCell alloc]initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:identifierH];
                
            }
            tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
            NSString *str = [NSString stringWithFormat:@"%d",indexPath.section-1];
            NSMutableArray *array = _HFdic[str];
            MLZHFModel*model  = array[indexPath.row-1];
            cell.textLabel.text = [NSString stringWithFormat:@"%@:%@",model.userName,model.contents];
            cell.textLabel.adjustsFontSizeToFitWidth= YES;
        
        
            return cell;
        }
    
    }

}
-(void)HFbuttonAction:(UIButton*)button{

   
    MLZCommunityHFViewController *HF = [[MLZCommunityHFViewController alloc]init];
    HF.modelC = _listArray[button.tag-200];
    HF.index = button.tag-200 ;
    HF.objectID = _model.objectId;
    [self.navigationController pushViewController:HF animated:YES];
    
    


}
//设置分区表头
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
  
    
    
   
        if(section==0){
            return 70;
        }else {
            
            
                return 0;
                
            
            
          
        
        }
}
-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{

    if(section==0){
    MLZCommuityDetalisView *view = [[MLZCommuityDetalisView alloc]initWithFrame:CGRectMake(0, 0, KW, 70)];
        view.backgroundColor = [UIColor whiteColor];
        view.labeleTitle.text = _model.title;
        view.lableTime.text = _model.updatedAt;
        view.lableNumber.text = @"评论人数:0";
        return view;
    }else {
    
        return nil;
    
    }
}
//回复高度
-(void)FHheightForHeaderView:(CGFloat)height{
    HForHead = height;
   


}
//表尾
//-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
//
//
//    return 3;
//
//}


#pragma mark------布局帖子详细界面 下面是评论界面
- (void)talkBtnClick:(UITextView *)textViewGet{
    
   

    
    if([BmobUser getCurrentUser]){
        NSUserDefaults *userqq = [NSUserDefaults standardUserDefaults];
        NSString*name = [userqq objectForKey:@"username"];
        NSString*imageIcon = [userqq objectForKey:@"imageIcon"];
        if([[BmobUser getCurrentUser].username isEqualToString:name]){
        
     BmobUser *user = [BmobUser getCurrentUser];
       
     NSString *ClassName = [NSString stringWithFormat:@"P%@ACM_", _model.objectId];
    
   
    

    BmobObject*object  = [BmobObject objectWithClassName:ClassName];
    [object setObject:textViewGet.text forKey:@"contents"];
    [object setObject:user.username forKey:@"userName"];
    //[object setObject:_titlefild.text forKey:@"title"];
    [object setObject:imageIcon forKey:@"picture"];
    [object saveInBackgroundWithResultBlock:^(BOOL isSuccessful, NSError *error) {
            if(isSuccessful){
                
            
            
                 [self performSelector:@selector(shuxin) withObject:nil afterDelay:1];
                
            }
            
            // 1秒消失
            
            
            
            
        }];
        
        
        
        
    }else{
    
    
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请登录" delegate: self cancelButtonTitle:nil otherButtonTitles: nil];
        [alertView show];
        
         ///1秒消失
         [self performSelector:@selector(alertViewDismiss:) withObject:alertView afterDelay:1];
    
    }
    
    
}
    
    
    
    
    
    
    
    
    
    
    
    
    }
//有个提示 跟刷新冲突
- (void)alertViewDismiss:(UIAlertView *)alertView
{
    
    [alertView dismissWithClickedButtonIndex:0 animated:YES];
    
    
}
-(void)shuxin{


    
    [self datapassing];
   


}


//图片
- (void)IMButtonAction{
    
    
       
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
